package com.demo.androidgame.finsternis;

import java.util.List;

import javax.microedition.khronos.opengles.GL10;

import com.demo.androidgame.fx.Game;
import com.demo.androidgame.fx.Input.TouchEvent;
import com.demo.androidgame.fx.gl.Camera2D;
import com.demo.androidgame.fx.gl.SpriteBatcher;
import com.demo.androidgame.fx.gl.Texture;
import com.demo.androidgame.fx.gl.TextureRegion;
import com.demo.androidgame.fx.impl.GLScreen;
import com.demo.androidgame.fx.math.OverlapTester;
import com.demo.androidgame.fx.math.Rectangle;
import com.demo.androidgame.fx.math.Vector2;

public class CreditScreen extends GLScreen {
    Camera2D guiCam;
    SpriteBatcher batcher;
    Rectangle mainmenuBounds;
    Vector2 touchPoint;
    Texture creditImage, creditfont;	
    TextureRegion creditRegion, creditfontRegion; 
    
    public CreditScreen(Game game) {
        super(game);
        
        guiCam = new Camera2D(glGraphics, 480, 320);
        mainmenuBounds = new Rectangle(480 - 5 - 150, 5+10, 150, 40); //480-52, 20, 89, 20
        touchPoint = new Vector2();
        batcher = new SpriteBatcher(glGraphics, 1);
    }
    
    @Override
    public void resume() {
    	creditImage = new Texture(glGame, "credit.png" );   			
    	creditRegion = new TextureRegion(creditImage, 0, 0, 480, 320);
    	creditfont = new Texture(glGame, "creditfont.png" );   			
    	creditfontRegion = new TextureRegion(creditfont, 0, 0, 270, 120);
    }
    
    @Override
    public void pause() {
    	creditImage.dispose();
    	creditfont.dispose();
    }

    @Override
    public void update(float deltaTime) {
        List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
        game.getInput().getKeyEvents();
        int len = touchEvents.size();
        for(int i = 0; i < len; i++) {
            TouchEvent event = touchEvents.get(i);
            touchPoint.set(event.x, event.y);
            guiCam.touchToWorld(touchPoint);
            
            if(event.type == TouchEvent.TOUCH_UP) {
                if(OverlapTester.pointInRectangle(mainmenuBounds, touchPoint)) {
                    Assets.playSound(Assets.clickSound);
                    game.setScreen(new MainMenuScreen(game)); 
                    return;
                }
            }
        }
    }

    @Override
    public void present(float deltaTime) {
        GL10 gl = glGraphics.getGL();        
        gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        guiCam.setViewportAndMatrices();
        
        gl.glEnable(GL10.GL_TEXTURE_2D);
        
        batcher.beginBatch(creditImage);		
        batcher.drawSprite(240, 160, 480, 320, creditRegion);  
        batcher.endBatch();
        
        batcher.beginBatch(creditfont);		
        batcher.drawSprite(240, 160, 270, 120, creditfontRegion);  
        batcher.endBatch();
        
        gl.glEnable(GL10.GL_BLEND);
        gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
        
        batcher.beginBatch(Assets.gamefont);          
        batcher.drawSprite(480-52, 20, 89, 20, Assets.mainMenu);
        batcher.endBatch();
        
        gl.glDisable(GL10.GL_BLEND);
        
        gl.glDisable(GL10.GL_LIGHT1);
		gl.glDisable(GL10.GL_LIGHTING);
    }

    @Override
    public void dispose() {
    }
}
